package com.znjc.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.znjc.common.constant.UserConstants;
import com.znjc.device.domain.DeviceTypeSerialRef;
import com.znjc.device.mapper.DeviceTypeSerialRefMapper;
import com.znjc.device.service.IDeviceTypeSerialRefService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zengpc
 * @date 2023-12-04
 */
@Service
public class DeviceTypeSerialRefServiceImpl extends ServiceImpl<DeviceTypeSerialRefMapper, DeviceTypeSerialRef> implements IDeviceTypeSerialRefService {

    @Override
    public boolean checkDeviceTypeSerialRefById(Long id, int tag) {
        LambdaQueryWrapper<DeviceTypeSerialRef> wrapper = new LambdaQueryWrapper<>();
        if (1 == tag) {
            wrapper.eq(DeviceTypeSerialRef::getTypeId, id);
        } else {
            wrapper.eq(DeviceTypeSerialRef::getSerialId, id);
        }
        int count = this.count(wrapper);
        if (count > 0) {
            return UserConstants.UNIQUE;
        }
        return UserConstants.NOT_UNIQUE;
    }

    @Override
    public List<Long> getIdList(Long id, int tag) {
        LambdaQueryWrapper<DeviceTypeSerialRef> wrapper = new LambdaQueryWrapper<>();
        if (tag == 0) {
            wrapper.eq(DeviceTypeSerialRef::getTypeId, id);
        } else {
            wrapper.eq(DeviceTypeSerialRef::getSerialId, id);
        }
        List<DeviceTypeSerialRef> list = this.list(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            if (tag == 0) {
                return list.stream().map(DeviceTypeSerialRef::getSerialId).collect(Collectors.toList());
            } else {
                return list.stream().map(DeviceTypeSerialRef::getTypeId).collect(Collectors.toList());
            }
        }
        return null;
    }

    @Override
    public Long getSerialId(Long typeId) {
        LambdaQueryWrapper<DeviceTypeSerialRef> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeviceTypeSerialRef::getTypeId, typeId);
        wrapper.last("limit 0,1");
        DeviceTypeSerialRef ref = this.getOne(wrapper);
        return null == ref ? null : ref.getSerialId();
    }
}
